from django.shortcuts import render
from django.http import HttpResponse,JsonResponse

from django.contrib.auth.models import User,Permission,Group

from django.core.urlresolvers import reverse
from django.contrib.auth import authenticate, login,logout

from django.contrib.auth.decorators import permission_required

# 管理员用户添加
def useradd(request):
    if request.method == 'GET':
        # 获取所有的组
        glist = Group.objects.all()

        context = {'glist':glist}
    
        return render(request,'auth/user/add.html',context)

    elif request.method == 'POST':
        # 执行管理员添加
        # ob = User.objects.all()
        # print(ob)
        
        # 判断是否创建超级管理员
        if request.POST['is_superuser'] == '1':
            ob = User.objects.create_superuser(request.POST['username'],request.POST['email'],request.POST['password'])
        else:
            ob = User.objects.create_user(request.POST['username'],request.POST['email'],request.POST['password'])
        # 进行添加
        ob.save()

        # 判断是否需要为用户分配组  gs组的id号
        gs = request.POST.getlist('gs',None)
        print(gs)

        if gs:
            # 给当前用户分配组
            ob.groups.set(gs)
            ob.save()

        return HttpResponse('<script>location.href="'+reverse('auth_user_list')+'"</script>')
    


# 管理员用户列表
def userlist(request):
    # 获取所有的管理员
    data = User.objects.all()

    context = {'ulist':data}

    return render(request,'auth/user/list.html',context)


# 管理员用户删除
def userdel(request,uid):
    
    ob = User.objects.get(id=uid)
    ob.delete()

    return HttpResponse('<script>location.href="'+reverse('auth_user_list')+'"</script>')
    

# 管理组添加
def groupadd(request):
    if request.method == 'GET':
        # 获取所有权限
        perms = Permission.objects.exclude(name__istartswith='Can')

        context = {'perms':perms}

        return render(request,'auth/group/add.html',context)

    elif request.method == 'POST':

        # 创建组
        g = Group(name=request.POST['name'])
        g.save()

        # 获取所有选中的权限
        prms = request.POST.getlist('prms',None)

        # 判断是否需要给组添加权限
        if prms:
            # 给组分配权限
            g.permissions.set(prms)

            g.save()

        return HttpResponse('<script>location.href="'+reverse('auth_group_list')+'"</script>')

# 管路组列表
def grouplist(request):

    # 获取所有的组
    data =  Group.objects.all()

    context = {'glist':data}

    return render(request,'auth/group/list.html',context)

# 编辑组
def groupedit(request,gid):

  # 获取当前组的信息
    ginfo = Group.objects.get(id=gid)

    # 如果查看读取数据
    if request.method == 'GET':
        # print(ginfo.permissions.all())
        # 读取权限,排除已有的权限
        perms = Permission.objects.exclude(group=ginfo).exclude(name__istartswith='Can')
        # 分配数据
        context = {'ginfo':ginfo,'perms':perms}

        return render(request,'auth/group/edit.html',context)

    elif request.method == 'POST':
        # 修改组名
        ginfo.name =request.POST['name']

        # 判断是否有权限
        prms = request.POST.getlist('prms',None)

        # 全部删除,
        ginfo.permissions.clear()

        if prms:
            # 再添加
            ginfo.permissions.set(prms)
        
        # 保存
        ginfo.save()


        return HttpResponse('<script>location.href="'+reverse('auth_group_list')+'"</script>')





